package com.qf.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.openapi.web.master.dao.SystemParamDao;
import com.qf.openapi.web.master.entity.SystemParam;
import com.qf.openapi.web.master.service.SystemParamService;
import com.qf.openapi.web.master.utils.Constants;
import com.qf.openapi.web.master.vo.ResultVo;
import com.qf.openapi.web.master.vo.TableVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SystemParamServiceImpl implements SystemParamService {

    @Autowired
    SystemParamDao dao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public TableVo<SystemParam> getallParams(Integer page, Integer limit ,SystemParam systemParam) {
        PageHelper.startPage(page, limit);
        PageInfo<SystemParam> info = new PageInfo<>();
        List<SystemParam> list = dao.selectAllParams(systemParam);
        if ( list.size() > 0) {
            int l = (int) info.getTotal();
            TableVo<SystemParam> tableVo = TableVo.ok(l, list);
            return tableVo;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addParams(SystemParam systemParam) {
        Integer i =  dao.addParams(systemParam);
        if (i == 1){
            return new ResultVo("OK", Constants.SUCCESS);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo updateParams(SystemParam systemParam) {
        Integer i = dao.updateParams(systemParam);
        if (i == 1){
            return new ResultVo("OK", Constants.SUCCESS);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo deleteParams(List ids) {
        Integer i =  dao.deleteParams(ids);
        if (i != 0) {
            return new ResultVo("OK", Constants.SUCCESS);
        }
        return null;
    }
}
